package com.cn.gmall.cart.controller;

import com.cn.gmall.cart.service.CartInfoService;
import com.cn.gmall.common.constant.RedisConst;
import com.cn.gmall.common.execption.GmallException;
import com.cn.gmall.common.result.Result;
import com.cn.gmall.common.result.ResultCodeEnum;
import com.cn.gmall.common.util.AuthContextHolder;
import com.cn.gmall.model.cart.CartInfo;
import org.apache.http.conn.util.PublicSuffixList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.management.relation.RelationSupport;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartApiController {

    @Autowired
    private CartInfoService cartInfoService;

//    h5 端 购物车的全选 全不选








    /**
     * 获取选中状态的购物车列表
     *   /api/cart/getCartCheckedList/{userId}
     * @param userId
     * @return
     */

    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){

        return  cartInfoService.getCartCheckedList(userId);
    }

    /***
     * deleteIsChecked
     * 删除购物车 选中的购物项
     *
     * @return
     */

    @DeleteMapping("/deleteIsChecked")
    public Result deleteIsChecked(HttpServletRequest request){
        String userId = getUserIdByUserIdOrUserTemId(request); //获取userId
cartInfoService.deleteIsChecked(userId);
        return  Result.ok();
    }

    /***    /api/cart/deleteCart/{skuId}
     * 删除购物项
     * @param skuId
     * @return
     */

    @DeleteMapping("/deleteCart/{skuId}")
    public  Result deleteCart(@PathVariable String skuId,HttpServletRequest request){
//        用string 接收skuId 方便操作redis
        String userId = getUserIdByUserIdOrUserTemId(request);
        cartInfoService.deleteCart(skuId,userId);
        return  Result.ok();
    }


    /**
     *  /api/cart/checkCart/{skuId}/{isChecked}
     *  更新选中状态
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request) {
//        获取userId
        String userId = getUserIdByUserIdOrUserTemId(request);

        cartInfoService.checkCart(skuId, isChecked, userId);
        return Result.ok();
    }

    /**
     *
     * 从request里面的userId(用户登录) 或者userTempId(用户未登录)作用当前操作的userId 以执行后续操作
     * @param request
     * @return
     */
    private static String getUserIdByUserIdOrUserTemId(HttpServletRequest request) {
        //        先获取userId
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
            userId = AuthContextHolder.getUserTempId(request);
        }
        return userId;
    }


    /***查询购物车列表
     * /api/cart/cartList
     *
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request) {

        String userId = AuthContextHolder.getUserId(request);
        String userTempId = AuthContextHolder.getUserTempId(request);

        List<CartInfo> cartInfoList = cartInfoService.getCartList(userId, userTempId);

        return Result.ok(cartInfoList);
    }

    /***添加购物车
     * /api/cart/addToCart/{skuId}/{skuNum}
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable Integer skuNum, HttpServletRequest request) {
//       userId存在 返回userId id不存在 返回 ""
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)) {
//             没有登录 加入购物车用 临时userTemId
            userId = AuthContextHolder.getUserTempId(request);
        }
        if (StringUtils.isEmpty(userId)) {
            throw new GmallException("添加购物车 userId不能为空 ", ResultCodeEnum.FAIL.getCode());
        }

//        加入购物车
        cartInfoService.addToCart(userId, skuId, skuNum);


        return Result.ok();
    }
}
